package IMIC.NGUYENVANDOAN.SERIZABLEFILES.EXCERCISES;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

public class MODELS<T> {

    public boolean WriteListObjectIntoFiles(T lisValue, String pathFile) throws Exception {
        try {
            File objFile = new File(pathFile);
            FileOutputStream objOutFile = new FileOutputStream(objFile);
            ObjectOutputStream objOoS = new ObjectOutputStream(objOutFile);
            objOoS.writeObject(lisValue);
            objOoS.flush();
        } catch (Exception e) {
            throw e;
        }
        return true;
    }

    public T ReadListObjectIntoFiles(String pathFile) throws Exception {
        T lisValue = null;
        try {
            File objFile = new File(pathFile);
            ObjectInputStream objObjectIn = new ObjectInputStream(new FileInputStream(objFile));
            lisValue = (T) objObjectIn.readObject();
        } catch (Exception e) {
            throw e;
        }
        return lisValue;
    }

    public DefaultTableModel getModels(List<String> lisField, List<Vector<String>> lisRows) throws Exception {
        // 1. Khởi tạo đối tượng DefaultTableModel chứa thông tin Hàng và Cột
        DefaultTableModel objDTM = new DefaultTableModel();
        Vector<String> col_name = new Vector<String>();
        Vector<String> row = null;

        // 2. Khởi tạo thông tin column
        for (String strValue : lisField) {
            col_name.addElement(strValue);
        }
        objDTM.setColumnIdentifiers(col_name);

        // 3. Duyệt và khởi tạo thông tin Row
        for (int i = 0; i < lisRows.size(); i++) {
            row = new Vector<String>();
            row = lisRows.get(i);
            objDTM.addRow(row);
        }
        return objDTM;
    }
}
